package org.ascollada.core
{
	import org.ascollada.ASCollada;
	import org.ascollada.core.log.DaeLogger;
	
	public class DaeMesh extends DaeElement
	{
		/** */
		public var primitives	:Array;
		
		/** */
		public var sources		:Object;
		
		/**
		 * 
		 */ 	
		public function DaeMesh( document:DaeDocument, node:XML=null )
		{
			super( document, node );
		}
		
		/**
		 * Reads the node.
		 * 
		 * @param	node
		 */ 
		public override function read(node:XML):void
		{
			if(node.name().localName != ASCollada.DAE_MESH_ELEMENT)
			{
				DaeLogger.error("Not a " + ASCollada.DAE_MESH_ELEMENT + " element!");
				throw new Error("Not a " + ASCollada.DAE_MESH_ELEMENT + " element!");
			}
			
			super.read( node );
				
			this.primitives = new Array();
			this.sources = new Object();
			
			var children:XMLList = node.children();
			var numChildren:int = children.length();
			
			for(var i:int = 0; i < numChildren; i++)
			{
				var child:XML = children[ i ];
				
				switch( child.name().localName )
				{
					case ASCollada.DAE_SOURCE_ELEMENT:
						var source : DaeSource = new DaeSource( this.document, child );
						this.sources[ source.id ] = source;
						break;
					
					case ASCollada.DAE_VERTICES_ELEMENT:
						var vertices : DaeVertices = new DaeVertices( this.document, child );
						if ( vertices.input && this.sources[ vertices.input.source ] )
						{
							this.sources[ vertices.id ] = this.sources[ vertices.input.source ];
						}
						else
						{
							DaeLogger.error("DaeMesh: no " + ASCollada.DAE_VERTICES_ELEMENT + " defined!");
						}
						break;
					
					case ASCollada.DAE_POLYGONS_ELEMENT:
						var primitive : DaePrimitive = new DaePrimitive( this.document, child );
						this.primitives.push( primitives );
						break;
							
					default:
						break;
				}
			}
		}
	}
}